home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / J_AUTHOR / Mojo / data.2 / docs / microline / examples / tree4.java < prev   
Encoding:
Java Source  |  1996-05-27  |  6.8 KB  |  219 lines

  1. // (c) Copyright 1994-1996 Microline Software, Inc. ALL RIGHTS RESERVED
  2. //
  3. // THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE COPIED AND USED
  4. // ONLY IN ACCORDANCE WITH THE TERMS OF THAT LICENSE AND WITH THE INCLUSION
  5. // OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE AND DOCUMENTATION, AND ITS
  6. // COPYRIGHTS ARE OWNED BY MICROLINE SOFTWARE AND ARE PROTECTED BY UNITED
  7. // STATES COPYRIGHT LAWS AND INTERNATIONAL TREATY PROVISIONS.
  8. //
  9. // THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  10. // AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY MICROLINE SOFTWARE.
  11. //
  12. // THIS SOFTWARE AND REFERENCE MATERIALS ARE PROVIDED "AS IS" WITHOUT
  13. // WARRANTY AS TO THEIR PERFORMANCE, MERCHANTABILITY, FITNESS FOR ANY
  14. // PARTICULAR PURPOSE, OR AGAINST INFRINGEMENT.  MICROLINE SOFTWARE
  15. // ASSUMES NO RESPONSIBILITY FOR THE USE OR INABILITY TO USE THIS
  16. // SOFTWARE.
  17. //
  18. // MICROLINE SOFTWARE SHALL NOT BE LIABLE FOR INDIRECT, SPECIAL OR
  19. // CONSEQUENTIAL DAMAGES RESULTING FROM THE USE OF THIS PRODUCT. SOME
  20. // STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR
  21. // CONSEQUENTIAL DAMAGES, SO THE ABOVE LIMITATIONS MIGHT NOT APPLY TO
  22. // YOU.
  23. //
  24. // MICROLINE SOFTWARE SHALL HAVE NO LIABILITY OR RESPONSIBILITY FOR SOFTWARE
  25. // ALTERED, MODIFIED, OR CONVERTED BY YOU OR A THIRD PARTY, DAMAGES
  26. // RESULTING FROM ACCIDENT, ABUSE OR MISAPPLICATION, OR FOR PROBLEMS DUE
  27. // TO THE MALFUNCTION OF YOUR EQUIPMENT OR SOFTWARE NOT SUPPLIED BY
  28. // MICROLINE SOFTWARE.
  29. //
  30. // U.S. GOVERNMENT RESTRICTED RIGHTS
  31. // This Software and documentation are provided with RESTRICTED RIGHTS.
  32. // Use, duplication or disclosure by the Government is subject to
  33. // restrictions as set forth in subparagraph (c)(1) of the Rights in
  34. // Technical Data and Computer Software Clause at DFARS 252.227-7013 or
  35. // subparagraphs (c)(1)(ii) and (2) of Commercial Computer Software -
  36. // Restricted Rights at 48 CFR 52.227-19, as applicable, supplier is
  37. // Microline Software, 41 Sutter St Suite 1374, San Francisco, CA 94104.
  38.  
  39. import java.awt.*;
  40. import java.util.*;
  41. import java.mct.*;
  42. import java.applet.Applet;
  43.  
  44. class DirData
  45. {
  46. public String path;
  47. boolean loaded;
  48.  
  49. public DirData(String path)
  50.     {
  51.     this.path = path;
  52.     loaded = false;
  53.     }
  54. }
  55.  
  56. public class tree4 extends Applet
  57. {
  58. MlGrid grid;
  59. MlTree tree;
  60.  
  61. public void init()
  62.     {
  63.     MlResources res;
  64.     int w, rw;
  65.  
  66.     res = new MlResources();
  67.     setLayout(null);
  68.     setBackground(new Color(255, 255, 255));
  69.  
  70.     tree = new MlTree();
  71.  
  72.     tree.setValue("autoSelect", false);
  73.  
  74.     // add a single row containing the root path to the Tree
  75.     tree.addRow(1, true, false, 0, null, "/");
  76.     res.add("row", 0);
  77.     res.add("rowUserObject", new DirData("/"));
  78.     tree.setValues(res);
  79.  
  80.     grid = new MlGrid();
  81.     res.clear();
  82.     res.add("columns", 3);
  83.     res.add("simpleWidths", "16c 12c 10c");
  84.     res.add("simpleHeadings", "Name|Type|Size");
  85.     res.add("allowColumnResize", true);
  86.     res.add("headingRows", 1);
  87.     res.add("shadowThickness", 0);
  88.     grid.setValues(res);
  89.  
  90.     res.clear();
  91.     res.add("cellDefaults", true);
  92.     res.add("cellBackground", "#ffffff");
  93.     res.add("cellTopBorderType", "BORDER_NONE");
  94.     res.add("cellBottomBorderType", "BORDER_NONE");
  95.     res.add("cellRightBorderType", "BORDER_NONE");
  96.     res.add("cellLeftBorderType", "BORDER_NONE");
  97.     res.add("cellAlignment", "ALIGNMENT_LEFT");
  98.     grid.setValues(res);
  99.  
  100.     res.clear();
  101.     res.add("cellDefaults", true);
  102.     res.add("column", 2);
  103.     res.add("cellAlignment", "ALIGNMENT_RIGHT");
  104.     grid.setValues(res);
  105.  
  106.     w = bounds().width / 2 - 52;
  107.     tree.reshape(0, 0, w, bounds().height);
  108.     add(tree);
  109.  
  110.     rw = bounds().width - w - 4;
  111.     grid.reshape(w + 4, 0, rw, bounds().height);
  112.     add(grid);
  113.  
  114.     // invoke the select callback for the first row in the Tree
  115.     // to fill the Grid with the data for the root path
  116.     tree.selectRow(0, true);
  117.     }
  118.  
  119. public boolean handleEvent(Event event)
  120.     {
  121.     MlTreeEvent treeEvent;
  122.     boolean handled;
  123.  
  124.     handled = false;
  125.     if (event.target == tree && event.id == MlTreeEvent.EXPAND_ROW)
  126.         {
  127.         treeEvent = (MlTreeEvent)event;
  128.         expandRow(treeEvent.row);
  129.         handled = true;
  130.         }
  131.     else if (event.target == tree && event.id == MlGridEvent.SELECT_ROW)
  132.         {
  133.         treeEvent = (MlTreeEvent)event;
  134.         selectRow(treeEvent.row);
  135.         handled = true;
  136.         }
  137.     if (handled)
  138.         return true;
  139.     return super.handleEvent(event);
  140.     }
  141.  
  142. protected void expandRow(int r)
  143.     {
  144.     MlResources res;
  145.     int i, pos;
  146.     DirData data;
  147.     String name;
  148.     Integer level;
  149.  
  150.     // retrieve the DirData of the directory to expand.  This is kept
  151.     // in the row's rowUserObject
  152.     data = (DirData)tree.getRowValue(MlTree.CONTENT, r, "rowUserObject");
  153.     if (data.loaded)
  154.         return; // children already loaded
  155.  
  156.     res = new MlResources();
  157.     level = (Integer)tree.getRowValue(MlTree.CONTENT, r, "rowLevel");
  158.     // in a real application, we would take the path and read its
  159.     // sub-directories and add it to the tree.  since we have no
  160.     // data source, we fake one here
  161.     tree.setValue("layoutFrozen", true);
  162.     pos = r + 1;
  163.     for (i = 0; i < 6; i++)
  164.         {
  165.         name = null;
  166.         switch (i)
  167.             {
  168.             case 0: name = "etc";   break;
  169.             case 1: name = "bin";   break;
  170.             case 2: name = "cal";  break;
  171.             case 3: name = "doc";   break;
  172.             case 4: name = "pub";   break;
  173.             case 5: name = "web";  break;
  174.             case 6: name = "tmp";   break;
  175.             }
  176.         tree.addRow(level.intValue() + 1, true, false, pos, null, name);
  177.         res.clear();
  178.         res.add("row", pos);
  179.         res.add("rowUserObject", new DirData(data.path + "/" + name));
  180.         tree.setValues(res);
  181.         pos++;
  182.         }
  183.     tree.setValue("layoutFrozen", false);
  184.     data.loaded = true;
  185.     }
  186.  
  187. protected void selectRow(int r)
  188.     {
  189.     DirData data;
  190.     int i, pos;
  191.  
  192.     // retrieve the directory selected
  193.     data = (DirData)tree.getRowValue(MlTree.CONTENT, r, "rowUserObject");
  194.  
  195.     // in a real application, we would take the selected directory
  196.     // and load in its sub-files and add it to the Grid.  since we have
  197.     // no data source, we fake one here
  198.     grid.setValue("layoutFrozen", true);
  199.     grid.deleteAllRows();
  200.     pos = 0;
  201.     for (i = 0; i < (r % 20) + 5; i++)
  202.         {
  203.         grid.addRows(1);
  204.         // add semi-random sample data to the Grid
  205.         if (r % 2 == i % 3)
  206.             grid.setStrings(MlGrid.CONTENT, pos, MlGrid.CONTENT, 0,
  207.                 "site_" + i + "|Site|1080");
  208.         else if (r % 3 == i % 3)
  209.             grid.setStrings(MlGrid.CONTENT, pos, MlGrid.CONTENT, 0,
  210.                 "directory_" + (i * r) + "|Directory|1030");
  211.         else
  212.             grid.setStrings(MlGrid.CONTENT, pos, MlGrid.CONTENT, 0,
  213.                 "file_" + (i + r) + "|file|" + (r * 40));
  214.         pos++;
  215.         }
  216.     grid.setValue("layoutFrozen", false);
  217.     }
  218. }
  219.